#include "sceneFileUpdate.h"
#include "accessPublishUtil.h"
#include "configParamUtil.h"
#include "param.h"
#include "log/Logging.h"
#include <set>
#include <thread>
#include <regex>


const string getCloudAddress()
{
    string sn = configParamUtil::getInstance()->getPanelInfo().getString("device_sn");
    smatch sm;
    if(regex_match(sn, sm, regex(".*TEST.*"))){
        LOG_INFO << "HongMeiCloudDomain: " << HongMeiCloudDomain_TEST;
        return HongMeiCloudDomain_TEST;
    }
    LOG_INFO << "HongMeiCloudDomain: " << HongMeiCloudDomain_FORMAL;
    return HongMeiCloudDomain_FORMAL;
}


bool get_familyDevs_fromCloud(const string cid, const string familyId, qlibc::QData &response)
{
    qlibc::QData cloudRequest, cloudResponse;
    cloudRequest.setString("cid", cid);
    cloudRequest.setString("familyId", familyId);
    if(access_service_hongmeiCloud(getCloudAddress(), GetFamilyDevs, cloudRequest, cloudResponse) && cloudResponse.getString("code") == "1000"){
        response.setInitData(cloudResponse.getData("data"));
        return true;
    }
    return false;
}


qlibc::QData get_panelDevs_fromDevData(qlibc::QData& devData, const string phone)
{
    qlibc::QData panelList;
    for(int i = 0; i < devData.size(); ++i){
        qlibc::QData devItem = devData.getArrayElement(i);
        if(devItem.getString("categoryid") == "VOICEGATEWAY"){
            qlibc::QData panelData;
            panelData.setString("category_code", "audiopanel");
            
            panelData.setString("device_mac", devItem.getData("devShadowData").getData("state").getData("reported").getString("mac"));
            panelData.setString("device_sn", devItem.getData("devShadowData").getData("state").getData("reported").getString("mac"));
            panelData.setString("device_name",devItem.getString("name"));
            panelData.setString("device_type", "2");
            panelData.setString("product_type", "语音面板");

            panelData.setString("phone", phone);
            panelData.setString("family_code", configParamUtil::getInstance()->getPanelInfo().getString("familyId"));
           
            panelData.setString("roomName", devItem.getString("roomName"));
            panelData.setString("room_name", devItem.getString("roomName"));
            panelData.setString("room_no", devItem.getString("roomId"));
            panelData.setString("coverArea", devItem.getString("roomId"));

            panelData.setString("state", "ok");
            panelData.setString("timeStamp", devItem.getData("devShadowData").getString("timestamp"));
            panelData.setString("createTime", devItem.getData("devShadowData").getString("timestamp"));

            panelList.append(panelData);
        }
    }
    return panelList;
}


/**
 * 出发更新条件：
 * 		* 面板、雷达、房间发生变化
 * 
 * 更新逻辑
 * 		1. 获取设备列表、获取房间列表
 * 		2. 构造面板、雷达、房间数据
 *      3. 用旧区域刷新雷达设备的coverArea
 *      4. 保存白名单
 * 		5. 向所有的雷达设备发送一条区域请求数据; 后续收到雷达的区域消息后，进行区域的处理
 */
bool refresh_whiteList()
{
    //从云端获取设备列表、获取房间列表
    string cid, familyId;
    qlibc::QData devData, roomData;
    bool isGetFromCloudSuccess = false;
    while(!isGetFromCloudSuccess){
        qlibc::QData panelInfo = configParamUtil::getInstance()->getPanelInfo();
        cid = panelInfo.getString("cid");
        familyId = panelInfo.getString("familyId");
        bool isGetDevData = get_familyDevs_fromCloud(cid, familyId, devData);
       
    }
    //获取面板列表
    // qlibc::QData voicePanelList = get_panelDevs_fromDevData(devData, phone);
    
   
    
    LOG_BLUE << "refresh_whiteList completed ...";
    return true;
}


